package com.example.shiro;

import com.alibaba.druid.pool.DruidDataSource;
import com.example.shiro.realm.CustomRealm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.SimpleAccountRealm;
import org.apache.shiro.realm.jdbc.JdbcRealm;
import org.apache.shiro.subject.Subject;

public class CustomRealmTest {

    CustomRealm customRealm = new CustomRealm();



    void testShiro(){
        //构建 SecurityManager
        DefaultSecurityManager securityManager = new DefaultSecurityManager();


        securityManager.setRealm(customRealm);

        //主体认证提交请求
        //用户传过来的数据   和reaml里面的进行匹配
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();

        //加密  加盐
        HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
        matcher.setHashAlgorithmName("md5");
        matcher.setHashIterations(1);
        customRealm.setCredentialsMatcher(matcher);

        UsernamePasswordToken token = new UsernamePasswordToken("admin","123456");
        //认证
        subject.login(token);


        //授权  是否有 user角色
        subject.checkRole("user");

        //资源
        subject.checkPermission("permit:delete");
        //
        subject.checkPermission("permit:update");

        System.out.println("admin is login : " + subject.isAuthenticated());
        subject.logout();
        System.out.println("admin is login : " + subject.isAuthenticated());



    }

    public static void main(String[] args) {

        //密码加密
        Md5Hash  md5Hash = new Md5Hash("123456","salt");
        System.out.println(md5Hash.toString());

        CustomRealmTest test = new CustomRealmTest();
        test.testShiro();
    }
}
